﻿package com.wandian.util
{
	import com.wandian.data.GameData;
	import com.wandian.util.scrollBar.ScrollBarMc;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;

    public class CLZLlistScrollPanel extends Sprite
    {
        private var num_w:int = 5;
        private var pic_w:int = 50;
        private var pic_h:int = 50;
        private var interval_w:int = 20;
        private var interval_h:int = 40;
        private var initX:int;
        private var initY:int;
        private var scrollX:int;
        private var scrollY:int;
        private var panelView:Sprite;
        private var _scrollBarMc:ScrollBarMc;
        private var maskH:int;

        public function CLZLlistScrollPanel(_num_w:int, _pic_w:int, _pic_h:int, _interval_w:int, _interval_h:int, _maskH:int, _scrollX:int = 0, _scrollY:int = 0, _initX:int = 0, _initY:int = 0)
        {
            this.num_w = _num_w;
            this.pic_w = _pic_w;
            this.pic_h = _pic_h;
            this.interval_w = _interval_w;
            this.interval_h = _interval_h;
            this.initX = _initX;
            this.initY = _initY;
            this.scrollX = _scrollX;
            this.scrollY = _scrollY;
            this.maskH = _maskH;
            this.panelView = new Sprite();
            this.addChild(this.panelView);
            return;
        }

        public function get canvas() : Sprite
        {
            return this.panelView;
        }

        public function setMc(mc:DisplayObject, index:int, total:int) : void
        {
            var btnScrollUp:SimpleButton = null;
            var btnScrollDown:SimpleButton = null;
            var btnScrollBg:SimpleButton = null;
            var mcScroll:MovieClip = null;
            var mcSign:MovieClip = null;
            var mwidth:int = 0;
            var point:Point = this.xy(index);
            this.panelView.addChild(mc);
            mc.x = point.x;
            mc.y = point.y;
//			trace("index:"+index+" x:"+point.x+" y:"+point.y);
            if (index == (total - 1))
            {
                btnScrollUp = GameData.getButton("ScrollUp");
                btnScrollDown = GameData.getButton("ScrollDown");
                btnScrollBg = GameData.getButton("ScrollBg");
                mcScroll = GameData.getMovieClip("Scroll");
                mcSign = GameData.getMovieClip("Sign");
                mwidth = this.num_w * this.pic_w + this.num_w * this.interval_w;
                this._scrollBarMc = new ScrollBarMc(new Rectangle(0, 0, mwidth, this.maskH), this.panelView, [btnScrollBg, btnScrollUp, btnScrollDown, mcScroll, mcSign]);
                if (this.scrollX == 0)
                {
                    this._scrollBarMc.x = this.panelView.x + mwidth;
                }
                else
                {
                    this._scrollBarMc.x = this.scrollX;
                }
                if (this.scrollY == 0)
                {
                    this._scrollBarMc.y = this.panelView.y;
                }
                else
                {
                    this._scrollBarMc.y = this.scrollY;
                }
            }
            return;
        }

        private function xy(index:int) : Point
        {
            var point:* = new Point();
            var yNum:int = index / this.num_w;
            var xNum:int = index % this.num_w;
            point.x = xNum * this.pic_w + this.interval_w * xNum + this.initX;
            point.y = yNum * this.pic_h + this.interval_h * yNum + this.initY;
            return point;
        }

        public function remove() : void
        {
            if (this._scrollBarMc != null)
            {
                this._scrollBarMc.disuse();
            }
            return;
        }

    }
}
